<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * SimplyPost
 *
 * @author		Pascal Kriete
 * @package		SimplyPost
 * @copyright	Copyright (c) 2008, Pascal Kriete
 * @license 	http://www.gnu.org/licenses/lgpl.txt
 */

// ------------------------------------------------------------------------

/**
 * Post Controller
 *
 * @package		SimplyPost
 * @subpackage	Post
 * @category	Controller
 * @author		Pascal Kriete
 */

class Post extends BaseController {

	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function Post()
	{
		parent::BaseController();
		$this->lang->load('post');
		$this->load->helper('date');
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * No real function - redirect
	 *
	 * @access	public
	 */
	function index()
	{
		$this->session->set_flashdata('msg', 'Post does not exist.');
		redirect('/');
	}
	
	// --------------------------------------------------------------------

	/**
	 * Show and validate reply form
	 *
	 * @access	public
	 * @param	integer	topic id
	 */
	function reply($id)
	{		
		set_title($this->lang->line('post_reply_title'));
		$this->breadcrumb->from_db($id, 'topic', 'Reply');
		
		$this->load->helper('form');
		$this->load->library('validation');
		$this->load->library('post_handler');
				
		$data = array(
			'form_target'	=> 'post/reply/'.$id,
			'submit_text'	=> 'Reply',
			'success_msg'	=> 'Post Added'
		);

		$this->post_handler->add_post($id, $data);
	}
	
	// --------------------------------------------------------------------

	/**
	 * Edit a post
	 *
	 * @access	public
	 * @param	integer	post id
	 * @param	integer	parent id
	 */
	function edit($id, $parent_id)
	{
		set_title($this->lang->line('post_edit_title'));
		$this->breadcrumb->from_db($id, 'post', 'Edit');
		
		$this->load->helper('form');
		$this->load->library('validation');
		$this->load->library('post_handler');
		
		$data = array(
			'form_target'	=> 'post/reply/'.$id,
			'submit_text'	=> 'Reply',
			'success_msg'	=> 'Post Added'
		);
		
		$this->post_handler->edit_post($id, $data);
	}
	
	// --------------------------------------------------------------------

	/**
	 * Validate new/edited post
	 *
	 * @access	public
	 * @param	string	post body
	 * @return	bool
	 */
	function _plugins($text)
	{
		$answer = $this->plugin->post_submit($text);
	
		if ($answer[0] === FALSE)
		{
			$this->validation->set_message('plugins', $answer[1]);
			return FALSE;
		}
		else
		{
			$_POST['content'] = $answer[1];
			return TRUE;
		}
	}

}